package ua.ck.example.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class ContextHolder {
	private static ContextHolder contextHolder = null;
	private ApplicationContext context = new FileSystemXmlApplicationContext(
			"src/main/webapp/WEB-INF/spring/root-context.xml");

	private ContextHolder() {
	}

	public static ContextHolder getInstance() {
		if (contextHolder == null) {
			contextHolder = new ContextHolder();
		}
		return contextHolder;
	}

	public ApplicationContext getContext() {
		return this.context;
	}

	public void setContext(ApplicationContext context) {
		this.context = context;
	}

	public static String getServerName() {
		InetAddress address = null;
		String name = null;
		try {
			address = InetAddress.getLocalHost();
			name = address.getHostName();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return name;
	}

	// public static void testMemcached() throws IOException {
	// MemcachedClient c = new MemcachedClient(new InetSocketAddress(
	// "127.0.0.1", 11211));
	// // Store a value (async) for one hour
	// c.set("key", 3600, "value");
	// // Retrieve a value (synchronously).
	// Object myObject = c.get("key");
	// System.out.println(myObject);
	// }

	public static void main(String[] args) {
		ContextHolder.getInstance();
		// BkInfoCollector bkInfoCollector = (BkInfoCollector)
		// ContextHolder.getInstance().getContext().getBean("bkInfoCollector");
		// try {
		// bkInfoCollector.call();
		// } catch (NumberFormatException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

	}
}
